package Thread;

import com.sun.scenario.effect.impl.prism.PrImage;

import java.util.concurrent.TimeUnit;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}15:50
 */
public class Profiler {
    //第一次get（）方法调用时会进行初始化（如果set（）方法没有被调用）
    private static final ThreadLocal<Long> TIME_THREDLOCAL=new ThreadLocal<Long>(){
        protected Long initialValue(){
            return System.currentTimeMillis();
        }
    };
    public static final void begin(){
        TIME_THREDLOCAL.set(System.currentTimeMillis());
    }

    public static final long end(){
        return System.currentTimeMillis()-TIME_THREDLOCAL.get();
    }

    public static void main(String[] args) throws InterruptedException {
        Profiler.begin();
        TimeUnit.SECONDS.sleep(1);
        System.out.println("Cost:"+Profiler.end());
    }
}
